package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.SetmealCheckgroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealCheckgroup;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Value("${imgpath.name}")
    public String url;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealCheckgroupMapper sCMapper;

    @Override
    public Result upload(MultipartFile imgFile) throws IOException {
        String lastName = imgFile.getOriginalFilename().substring(imgFile.getOriginalFilename().lastIndexOf("."));
        String uuid = UUID.randomUUID().toString();
        imgFile.transferTo(new File(url + uuid + lastName));
        return new Result(true, "图片上传成功", uuid + lastName);
    }

    @Override
    public Result add(Setmeal setmeal, Integer[] checkgroupIds) {
        //判断参数
        if (setmeal == null) {
            return new Result(false, "参数异常");
        }
        //添加检查套餐
        setmealMapper.insert(setmeal);
        //判断是否勾选检查组
        if (checkgroupIds.length != 0) {
            //如果勾选
            //根据checkgroupIds 获取勾选的检查组id
            for (Integer checkgroupId : checkgroupIds) {
                //添加套餐id和检查组id到数据库联表
                sCMapper.insert(new SetmealCheckgroup(setmeal.getId(), checkgroupId));
            }

        }
        return new Result(true, "添加套餐成功");
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //校验参数
        if (queryPageBean == null) {
            throw new RuntimeException("参数异常");
        }
        //创建Page对象
        IPage<Setmeal> p = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //判断是否有模糊查询
        if (queryPageBean.getQueryString() != null) {
            //如果有 判断条件是 编码、名称、 助记码
            wrapper.eq(Setmeal::getCode, queryPageBean.getQueryString()).or()
                    .like(Setmeal::getName, queryPageBean.getQueryString()).or()
                    .eq(Setmeal::getHelpCode, queryPageBean.getQueryString());
        }
        IPage<Setmeal> setmealIPage = setmealMapper.selectPage(p, wrapper);
        //封装进PageResult对象返回前端
        return new PageResult(setmealIPage.getTotal(), setmealIPage.getRecords());
    }

    @Override
    public Result findById(Integer id) {
        if (id == null) {
            return new Result(false, "参数异常");
        }
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getId, id);
        Setmeal setmeal = setmealMapper.selectOne(wrapper);
        return new Result(true, "查询套餐信息成功", setmeal);
    }

    @Override
    public Result edit(Integer[] checkgroupIds, Setmeal setmeal) {
        //修改套餐
        setmealMapper.updateById(setmeal);
        //删除套餐对应的原检查组信息
        LambdaUpdateWrapper<SetmealCheckgroup> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SetmealCheckgroup::getSetmealId, setmeal.getId());
        sCMapper.delete(wrapper);
        //添加对应新的检查组信息
        for (Integer checkgroupId : checkgroupIds) {
            sCMapper.insert(new SetmealCheckgroup(setmeal.getId(), checkgroupId));
        }
        return new Result(true, "修改套餐成功");
    }

    @Override
    public Result delete(Integer id) {
        //删除套餐对应检查组信息
        LambdaUpdateWrapper<SetmealCheckgroup> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SetmealCheckgroup::getSetmealId, id);
        sCMapper.delete(wrapper);
        //删除套餐
        setmealMapper.deleteById(id);
        return new Result(true, "删除套餐成功");
    }
}
